/*
 *  DataStructures.cpp
 *  c_Render
 *
 *  Created by liao peterson on 11/8/09.
 *  Copyright 2009 uestc. All rights reserved.
 *
 */
#ifndef STRUCTURES_CPP__
#define STRUCTURES_CPP__
#include "DataStructures.h"

template<class T>
Node<T>::Node()
{
	prev=NULL;
	next=NULL;
	data=NULL;
}

template<class T>
Node<T>::Node(Node<T>*Nprev,Node<T>*Nnext,T*Ndata)
{
	prev=Nprev;
	next=Nnext;
	data=Ndata;
}

template<class T>
Node<T>::~Node()
{
	//delete data;
}

template<class T>
List<T>::List()
{
	head=new Node<T>;
	tail=head;
	current=head;
	count=0;
	current_pos=-1;
}

template<class T>
List<T>::~List()
{
	for(int i=0;i<count;i++)
	{
		current=head->next;
		//delete head;
		head=current;
	}
}
template<class T>
void List<T>::Append(T*addOn)
{
	Node<T>*temp=new Node<T>(NULL,NULL,addOn);
	Append(temp);
}

template<class T>
void List<T>::Append(Node<T>*addOn)
{
	if(count==0)
	{
		head->next=addOn;
		current=addOn;
		current_pos=0;
	}
	addOn->prev=tail;
	addOn->next=NULL;
	tail->next=addOn;
	tail=tail->next;
	count++;
}

template<class T>
void List<T>::Dismiss()
{
	if(count<=0)
		return;
	Node<T>*temp=tail;
	tail=tail->prev;
	tail->next=NULL;
	if(current_pos>=count-1)
		current=tail;
	count--;
}

template<class T>
void List<T>::Dismiss(GLint index)
{
	if(index < -1 || index >= count)return;
	if(index != -1)
		SetCurrent(index);
	//if((index == count - 1) || ((index == -1) && (count == 1)))
	if(!current->next)
		Dismiss();
	else{
		Node<T>*temp = current;
		if(current == NULL || current->next == NULL)
		{
			GLfloat a =0;
			return;
		}
		current = current->next;
		temp->next->prev = temp->prev;
		temp->prev->next = temp->next;
		//delete temp;
		count--;
	}
}
		

template<class T>
void List<T>::SetCurrent(GLint index)
{
	if(index>=count||index<0)
		return;
	current_pos=index;
	current=head;
	for(int i=-1;i<index;i++)			//-1 for head, 0 for the first valid entry
		current=current->next;
}

#endif
